<?php
namespace app\admin\controller;

use app\model\ArticleModel;
use app\model\CategoryModel;
use think\facade\Request;
use think\facade\Validate;
use think\facade\View;

class Category extends Quit {
    //主页
    public function category_list(){
        parse_str(Request::query(),$queryParams); //把get参数给到$queryParams
        $cateforyList = CategoryModel::paginate(
            ['list_rows' =>2, //每页显示几条
            'query'=>$queryParams]); //实际上页面所有get参数
        View::assign('cateforyList',$cateforyList);
        return View::fetch();
    }

    //增加
    public function add(){
        return View::fetch();
    }

    public function add_Save(){
        //获取表单提交参数
        $name = Request::param();
        //参数验证
        $validate = Validate::rule([
            'category_name|分类名称' =>'require|min:2|max:40',
            'category_desc|分类内容' =>'require|min:2|max:255'
        ]);
        $result = $validate->check($name);
        if(!$result){
            echo $validate->getError();
            echo "<a href='index.php?s=admin/category/category_list'>返回主页面</a>";
            exit();
        }
        $create = CategoryModel::create([
            'category_name' =>$name['category_name'],
            'category_desc' =>$name['category_desc'],
            'update_time' =>time(),
            'add_time' =>time()
        ]);
        if($create){
            echo "<a href='/index.php?s=admin/category/category_list'>增加成功</a>";
            exit();
        }else{
            echo "<a href='/index.php?s=admin/category/category_list'>增加增加失败</a>";
            exit();
        }
    }

    // 修改
    public function update(){
        //获取表单提交参数
        $categoryid = Request::param('category_id');
        //参数验证
        $validate = Validate::rule([
            'category_id' => 'require|between:1,'.PHP_INT_MAX,
        ]);
        $result = $validate->check(['category_id'=>$categoryid]);
        if(!$result){
            echo $validate->getError();
            echo "<a href='index.php?s=admin/category/category_list'>返回主页面</a>";
            exit();
        }
        $cateforyList = CategoryModel::find($categoryid);
        if(!$cateforyList){
            echo "<a href='index.php?s=admin/category/category_list'>数据不存在,请返回主页面</a>";
            exit();
        }
        return View::fetch('',['cateforyList' =>$cateforyList]);
    }

    public function update_save(){

        $name = Request::param();

        $validate = Validate::rule([
            'category_id|分类id' => 'require|between:1,'.PHP_INT_MAX,
            'category_name|分类名称'=>'require|min:2|max:45',
            'category_desc|分类内容' =>'require|min:2|max:225'
        ]);
        $check = $validate->check($name);
        $id = $name['category_id'];
        if(!$check){
            echo $validate->getError();
            echo "<a href='/index.php?s=admin/category/update/category_id/$id'>返回上一页</a>";
            exit();
        }

        $result= CategoryModel::find($id);
        if(!$result){
            echo "<a href='index.php?s=admin/category/category_list'>操作失败，返回列表页面</a>";
            exit();
        }
        $result['category_name'] = $name['category_name'];
        $result['category_desc'] = $name['category_desc'];
        $result['update_time'] = time();
        $category = $result->save();
        if($category){
            echo "<a href='/index.php?s=admin/category/category_list'>操作成功,返回主页面</a>";
            exit();
        }else{
            echo "<a href='/index.php?s=admin/category/category_list'>操作失败,返回主页面</a>";
            exit();
        }
    }

    //删除

    public function delete(){
        $categoryid = Request::param("category_id");
        $validate = Validate::rule([
            'category_id' => 'require|between:1,'.PHP_INT_MAX
        ]);
        $check = $validate->check(['category_id' =>$categoryid]);
        if(!$check){
            echo $validate->getError();
            echo "<a href='/index.php?s=admin/category/category_list'>返回列表页面</a>";
            exit();
        }

        $category = CategoryModel::find($categoryid);
        if(!$category){
            echo "<a href='/index.php?s=admin/category/category_list'>数据为空,返回主页面</a>";
            exit();
        }
        $article = ArticleModel::where('category_id',$categoryid)->find();
        if($article){
            echo "<a href='/index.php?s=admin/category/category_list'>分类下有文章,不能删除，请返回主页面</a>";
            exit();
        }

        $result = $category->delete();
        if($result){
            echo "<a href='/index.php?s=admin/category/category_list'>操作成功,请返回主页面</a>";
            exit();
        }else{
            echo "<a href='/index.php?s=admin/category/category_list'>操作失败,请返回主页面</a>";
            exit();
        }
    }

    //退出登录

    public function quit(){
        $this->quit_list();
    }

    //全选删除
    public function omit(){
        $arr=array();
        $name =Request::param();
        foreach($name['checkbox'] as $row){
            $article = ArticleModel::getBycategory_id($row);
            if($article){
                $arr[] = $row;
            }
        }
        if($arr){
            echo "分类下有文章编号为:".implode(',',$arr);
            echo "<a href='index.php?s=admin/category/category_list'>返回分类页面</a>";
            exit();
        }
        $category = implode(',',$name['checkbox']);
        $resule = CategoryModel::destroy($category);
        if($resule){
            echo "<a href='index.php?s=admin/category/category_list'>操作成功，返回主页</a>";
            exit();
        }else{
            echo "<a href='index.php?s=admin/category/category_list'>操作失败,返回主页</a>";
        }
    }
}